#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{
    if ( argc != 2 )
    {
        printf("Pls input: ./a.out <src file name>\n");
        return -1;
    }

    FILE *fpSrc = fopen(argv[1], "r");
    if ( NULL == fpSrc )
    {
        perror("fail fopen");
        return -1;
    }

    int fdSrc = fileno(fpSrc);
    // no need to check validity of fdSrc based on fileno definition

    char buffer[100] = {0};
    ssize_t szReadRet = read(fdSrc, buffer, sizeof(buffer));
    printf("Total read %ld bytes from <%s> as below:\n", szReadRet, argv[1] );
    printf("%s",buffer);

    fclose(fpSrc);
    
    return 0;
}

/*
标准函数fopen打开文件流，
系统函数fileno函数获取文件描述符
系统函数read读取文件
标准函数fclose关闭文件流（同时关闭文件描述符，刷新缓存区） 
*/